using UnityEngine;
using UnityEngine.EventSystems;

namespace GameCore.Components
{
    public class UIDragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
    {
        public RectTransform panel;
        private Vector2 offset;
        private bool isDragging = false;

        void Start()
        {
            panel ??= GetComponent<RectTransform>();
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            panel.transform.SetAsLastSibling();
            panel.GetDragOffset(Global.UI.GetUILayer(0), eventData, out offset);
            isDragging = true;
        }

        public void OnDrag(PointerEventData eventData)
        {
            if (!isDragging)
                return;
            panel.DragUILimit(Global.UI.GetUILayer(0), eventData, offset);
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            isDragging = false;
        }
    }
}